﻿CREATE PROCEDURE [dbo].[LedgerTransTypesInsert]
(--типы проводок для экспорта в 1С
	@Name varchar(100),
	@DebitLedgerAccountId int,
	@DebitSubcontoTypeId1 int,
	@DebitSubcontoTypeId2 int,
	@DebitSubcontoTypeId3 int,
	@DebitSubcontoTypeId4 int,
	@DebitSubcontoTypeId5 int,
	@CreditLedgerAccountId int,
	@CreditSubcontoTypeId1 int,
	@CreditSubcontoTypeId2 int,
	@CreditSubcontoTypeId3 int,
	@CreditSubcontoTypeId4 int,
	@CreditSubcontoTypeId5 int,
	@Note varchar(250),
	@MD datetime OUTPUT,
	@MW varchar(50) OUTPUT,
	@Id int OUTPUT
)
AS
DECLARE @DepartmentUserId int
DECLARE @strMsg nvarchar(500), @intRet int, @intErrCode int
DECLARE @intNextId int
SET NOCOUNT ON

IF (@Name Is Null) OR (@DebitLedgerAccountId Is Null) OR (@CreditLedgerAccountId Is Null)
BEGIN
	SELECT @intErrCode = 1, @strMsg = 'Неправильные параметры в вызове процедуры ' + Object_Name(@@Procid) + ' !'
	RAISERROR (@strMsg, 11, @intErrCode)
	RETURN @intErrCode
END

IF (ltrim(@Name) = '')
BEGIN
	SELECT @intErrCode = 2, @strMsg = 'Незаполнено наименование!'
	RAISERROR (@strMsg, 11, @intErrCode)
	RETURN @intErrCode
END

IF EXISTS (SELECT TOP 1 ID FROM [CO].[LedgerTransTypes] WHERE Name = @Name AND ToDelete = 0) 
BEGIN
	SELECT @intErrCode = 3, @strMsg = 'Дублируется наименование!'
	RAISERROR (@strMsg, 11, @intErrCode)
	RETURN @intErrCode
END

IF NOT EXISTS (SELECT DepartmentId FROM PR.DepartmentOptions WHERE DepartmentId = 0 ) 
BEGIN
	SELECT @intErrCode = 4, @strMsg = 'Изменять данные спрвочников можно только в офисе!'
	RAISERROR (@strMsg, 11, @intErrCode)
	RETURN @intErrCode
END

IF @DebitSubcontoTypeId1 Is Null SET @DebitSubcontoTypeId1 = 1
IF @DebitSubcontoTypeId2 Is Null SET @DebitSubcontoTypeId2 = 1
IF @DebitSubcontoTypeId3 Is Null SET @DebitSubcontoTypeId3 = 1
IF @DebitSubcontoTypeId4 Is Null SET @DebitSubcontoTypeId4 = 1
IF @DebitSubcontoTypeId5 Is Null SET @DebitSubcontoTypeId5 = 1

IF @CreditSubcontoTypeId1 Is Null SET @CreditSubcontoTypeId1 = 1
IF @CreditSubcontoTypeId2 Is Null SET @CreditSubcontoTypeId2 = 1
IF @CreditSubcontoTypeId3 Is Null SET @CreditSubcontoTypeId3 = 1
IF @CreditSubcontoTypeId4 Is Null SET @CreditSubcontoTypeId4 = 1
IF @CreditSubcontoTypeId5 Is Null SET @CreditSubcontoTypeId5 = 1

EXEC [dbo].[ProcessStatistic] @Note OUTPUT, @MD OUTPUT, @MW OUTPUT
--получение значения идентификатора
EXEC @intRet = dbo.GetNextCounterFor 'Common', @intNextId OUTPUT
IF @intRet <> 0 RETURN @intRet

	SET NOCOUNT OFF;
INSERT INTO [CO].[LedgerTransTypes] (Id, [Name], 
[DebitLedgerAccountId],[DebitSubcontoTypeId1],[DebitSubcontoTypeId2],[DebitSubcontoTypeId3],[DebitSubcontoTypeId4],[DebitSubcontoTypeId5],
[CreditLedgerAccountId],[CreditSubcontoTypeId1],[CreditSubcontoTypeId2],[CreditSubcontoTypeId3],[CreditSubcontoTypeId4],[CreditSubcontoTypeId5],
[Note], [MD], [MW], ToDelete) 
VALUES (@intNextId, @Name,
@DebitLedgerAccountId,@DebitSubcontoTypeId1,@DebitSubcontoTypeId2,@DebitSubcontoTypeId3,@DebitSubcontoTypeId4,@DebitSubcontoTypeId5,
@CreditLedgerAccountId,@CreditSubcontoTypeId1,@CreditSubcontoTypeId2,@CreditSubcontoTypeId3,@CreditSubcontoTypeId4,@CreditSubcontoTypeId5,
@Note, @MD, @MW, 0);
	
--SELECT Id, [Name],
--[DebitLedgerAccountId],[DebitSubcontoTypeId1],[DebitSubcontoTypeId2],[DebitSubcontoTypeId3],[DebitSubcontoTypeId4],[DebitSubcontoTypeId5],
--[CreditLedgerAccountId],[CreditSubcontoTypeId1],[CreditSubcontoTypeId2],[CreditSubcontoTypeId3],[CreditSubcontoTypeId4],[CreditSubcontoTypeId5],
--Note, MD, MW, ToDelete
--FROM [CO].[LedgerTransTypes] WHERE (Id = @intNextId)

SELECT @Id = @intNextId

RETURN 0
